/**
 * 项目反馈控制器
 */
const BaseProjectController = require('./base_project_controller.js');
const ResearchService = require('../service/research_service.js');
const timeUtil = require('../../../framework/helpers/time_util.js');
const util = require('../../../framework/helpers/util.js');

class FeedbackController extends BaseProjectController {

	/** 业务层 */
	constructor() {
		super();
		this._service = new ResearchService();
	}

	/**
	 * 添加项目反馈
	 */
	async addFeedback() {
		// 数据校验
		let rules = {
			projectId: 'must|string|min:1|max:50|name=项目ID',
			title: 'must|string|min:2|max:100|name=反馈标题',
			content: 'must|string|min:5|max:5000|name=反馈内容',
			type: 'must|int|default=1|in:1,2,3,4,5|name=反馈类型',
			priority: 'must|int|default=2|in:1,2,3,4|name=优先级',
			customerId: 'must|string|min:1|max:50|name=客户ID',
			customerName: 'string|min:1|max:50|name=客户名称',
			images: 'array|name=反馈图片',
			files: 'array|name=反馈附件',
			businessUserId: 'string|min:1|max:50|name=业务对接人ID',
			technicalUserId: 'string|min:1|max:50|name=技术对接人ID',
			tags: 'array|name=标签'
		};

		// 取得数据
		const input = this.validateData(rules);

		// 附加数据
		const userId = this._userId;
		const userName = this._userName;

		// 构建数据
		const data = {
			FEEDBACK_ID: util.genRandomString(20),
			FEEDBACK_PROJECT_ID: input.projectId,
			FEEDBACK_TITLE: input.title,
			FEEDBACK_CONTENT: input.content,
			FEEDBACK_TYPE: input.type,
			FEEDBACK_PRIORITY: input.priority,
			FEEDBACK_STATUS: 1, // 默认待处理
			FEEDBACK_CUSTOMER_ID: input.customerId,
			FEEDBACK_CUSTOMER_NAME: input.customerName || '',
			FEEDBACK_IMAGES: input.images || [],
			FEEDBACK_FILES: input.files || [],
			FEEDBACK_BUSINESS_USER_ID: input.businessUserId || '',
			FEEDBACK_TECHNICAL_USER_ID: input.technicalUserId || '',
			FEEDBACK_TAGS: input.tags || [],
			FEEDBACK_ADD_USER_ID: userId,
			FEEDBACK_ADD_USER_NAME: userName,
			FEEDBACK_ADD_IP: this._ip
		};

		// 调用服务
		const result = await this._service.addProjectFeedback(data);

		return {
			code: 0,
			message: '添加成功',
			data: result
		};
	}

	/**
	 * 更新项目反馈
	 */
	async updateFeedback() {
		// 数据校验
		let rules = {
			id: 'must|string|min:1|max:50|name=反馈ID',
			title: 'string|min:2|max:100|name=反馈标题',
			content: 'string|min:5|max:5000|name=反馈内容',
			type: 'int|in:1,2,3,4,5|name=反馈类型',
			priority: 'int|in:1,2,3,4|name=优先级',
			status: 'int|in:1,2,3,4,5|name=状态',
			images: 'array|name=反馈图片',
			files: 'array|name=反馈附件',
			businessUserId: 'string|min:1|max:50|name=业务对接人ID',
			technicalUserId: 'string|min:1|max:50|name=技术对接人ID',
			handlerId: 'string|min:1|max:50|name=处理人ID',
			handlerName: 'string|min:1|max:50|name=处理人姓名',
			solution: 'string|min:1|max:5000|name=解决方案',
			solutionDate: 'int|name=解决日期',
			verificationResult: 'int|in:1,2|name=验证结果',
			verificationNotes: 'string|min:1|max:500|name=验证备注',
			verificationDate: 'int|name=验证日期',
			tags: 'array|name=标签',
			relatedTasks: 'array|name=关联任务'
		};

		// 取得数据
		const input = this.validateData(rules);

		// 构建数据
		const data = {
			FEEDBACK_EDIT_IP: this._ip
		};

		if (util.isDefined(input.title)) data.FEEDBACK_TITLE = input.title;
		if (util.isDefined(input.content)) data.FEEDBACK_CONTENT = input.content;
		if (util.isDefined(input.type)) data.FEEDBACK_TYPE = input.type;
		if (util.isDefined(input.priority)) data.FEEDBACK_PRIORITY = input.priority;
		if (util.isDefined(input.status)) data.FEEDBACK_STATUS = input.status;
		if (util.isDefined(input.images)) data.FEEDBACK_IMAGES = input.images;
		if (util.isDefined(input.files)) data.FEEDBACK_FILES = input.files;
		if (util.isDefined(input.businessUserId)) data.FEEDBACK_BUSINESS_USER_ID = input.businessUserId;
		if (util.isDefined(input.technicalUserId)) data.FEEDBACK_TECHNICAL_USER_ID = input.technicalUserId;
		if (util.isDefined(input.handlerId)) data.FEEDBACK_HANDLER_ID = input.handlerId;
		if (util.isDefined(input.handlerName)) data.FEEDBACK_HANDLER_NAME = input.handlerName;
		if (util.isDefined(input.solution)) data.FEEDBACK_SOLUTION = input.solution;
		if (util.isDefined(input.solutionDate)) data.FEEDBACK_SOLUTION_DATE = input.solutionDate;
		if (util.isDefined(input.verificationResult)) data.FEEDBACK_VERIFICATION_RESULT = input.verificationResult;
		if (util.isDefined(input.verificationNotes)) data.FEEDBACK_VERIFICATION_NOTES = input.verificationNotes;
		if (util.isDefined(input.verificationDate)) data.FEEDBACK_VERIFICATION_DATE = input.verificationDate;
		if (util.isDefined(input.tags)) data.FEEDBACK_TAGS = input.tags;
		if (util.isDefined(input.relatedTasks)) data.FEEDBACK_RELATED_TASKS = input.relatedTasks;

		// 调用服务
		await this._service.updateProjectFeedback(input.id, data);

		return {
			code: 0,
			message: '更新成功'
		};
	}

	/**
	 * 获取项目反馈详情
	 */
	async getFeedbackDetail() {
		// 数据校验
		let rules = {
			id: 'must|string|min:1|max:50|name=反馈ID'
		};

		// 取得数据
		const input = this.validateData(rules);

		// 调用服务
		const feedback = await this._service.getProjectFeedbackDetail(input.id);

		// 格式化日期
		if (feedback.FEEDBACK_ADD_TIME) feedback.FEEDBACK_ADD_TIME_FORMAT = timeUtil.timestamp2Time(feedback.FEEDBACK_ADD_TIME);
		if (feedback.FEEDBACK_SOLUTION_DATE) feedback.FEEDBACK_SOLUTION_DATE_FORMAT = timeUtil.timestamp2Time(feedback.FEEDBACK_SOLUTION_DATE);
		if (feedback.FEEDBACK_VERIFICATION_DATE) feedback.FEEDBACK_VERIFICATION_DATE_FORMAT = timeUtil.timestamp2Time(feedback.FEEDBACK_VERIFICATION_DATE);

		// 格式化评论日期
		if (feedback.FEEDBACK_COMMENTS && feedback.FEEDBACK_COMMENTS.length > 0) {
			feedback.FEEDBACK_COMMENTS.forEach(comment => {
				if (comment.time) comment.timeFormat = timeUtil.timestamp2Time(comment.time);
			});
		}

		return {
			code: 0,
			data: feedback
		};
	}

	/**
	 * 获取项目反馈列表
	 */
	async getFeedbackList() {
		// 数据校验
		let rules = {
			projectId: 'string|min:1|max:50|name=项目ID',
			status: 'int|name=状态',
			type: 'int|name=类型',
			priority: 'int|name=优先级',
			search: 'string|min:1|max:30|name=搜索关键词',
			page: 'must|int|default=1|name=当前页码',
			size: 'must|int|default=10|name=每页条数',
			sort: 'string|name=排序'
		};

		// 取得数据
		const input = this.validateData(rules);
		const page = input.page;
		const size = input.size;
		let orderBy = {
			FEEDBACK_ADD_TIME: 'desc'
		};

		// 处理排序
		if (input.sort) {
			const sortArr = input.sort.split('_');
			if (sortArr.length == 2) {
				const sortField = sortArr[0];
				const sortOrder = sortArr[1];
				const validFields = ['add', 'priority', 'status'];
				const validOrders = ['asc', 'desc'];
				if (validFields.includes(sortField) && validOrders.includes(sortOrder)) {
					orderBy = {};
					if (sortField === 'add') {
						orderBy.FEEDBACK_ADD_TIME = sortOrder;
					} else if (sortField === 'priority') {
						orderBy.FEEDBACK_PRIORITY = sortOrder;
					} else if (sortField === 'status') {
						orderBy.FEEDBACK_STATUS = sortOrder;
					}
				}
			}
		}

		// 构建查询条件
		const where = {};
		if (input.projectId) where.FEEDBACK_PROJECT_ID = input.projectId;
		if (util.isDefined(input.status)) where.FEEDBACK_STATUS = input.status;
		if (util.isDefined(input.type)) where.FEEDBACK_TYPE = input.type;
		if (util.isDefined(input.priority)) where.FEEDBACK_PRIORITY = input.priority;
		if (input.search) {
			const search = input.search.trim();
			where.FEEDBACK_TITLE = {
				$regex: search,
				$options: 'i' // 不区分大小写
			};
		}

		// 调用服务
		const result = await this._service.getProjectFeedbackList(where, {
			page,
			size,
			orderBy
		});

		// 格式化日期
		if (result.list && result.list.length > 0) {
			result.list.forEach(item => {
				if (item.FEEDBACK_ADD_TIME) item.FEEDBACK_ADD_TIME_FORMAT = timeUtil.timestamp2Time(item.FEEDBACK_ADD_TIME);
			});
		}

		return {
			code: 0,
			data: result
		};
	}

	/**
	 * 删除项目反馈
	 */
	async deleteFeedback() {
		// 数据校验
		let rules = {
			id: 'must|string|min:1|max:50|name=反馈ID'
		};

		// 取得数据
		const input = this.validateData(rules);

		// 调用服务
		await this._service.deleteProjectFeedback(input.id);

		return {
			code: 0,
			message: '删除成功'
		};
	}

	/**
	 * 添加项目反馈评论
	 */
	async addFeedbackComment() {
		// 数据校验
		let rules = {
			id: 'must|string|min:1|max:50|name=反馈ID',
			content: 'must|string|min:1|max:500|name=评论内容',
			type: 'int|default=1|in:1,2,3|name=评论类型'
		};

		// 取得数据
		const input = this.validateData(rules);

		// 构建评论数据
		const comment = {
			userId: this._userId,
			userName: this._userName,
			content: input.content,
			type: input.type || 1
		};

		// 调用服务
		await this._service.addProjectFeedbackComment(input.id, comment);

		return {
			code: 0,
			message: '评论添加成功'
		};
	}
}

module.exports = FeedbackController; 